public class Solution {
    
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        if (root == null)
            return res;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            LinkedList<Integer> l = new LinkedList<>();
            while (size != 0) {
                TreeNode node = q.poll();
                l.offer(node.val);
                size--;
                if (node.left != null){
                    q.offer(node.left);
                }
                if (node.right != null){
                    q.offer(node.right);
                }
            }
            res.add(l);
        }
        return res;
    }

    public List<List<Integer>> levelOrder0(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        if (root == null)
            return res;
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.add(root);
        Queue<Integer> hq = new LinkedList<Integer>();
        hq.add(0);
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            int height = hq.poll();
            if (node.left != null){
                q.add(node.left);
                hq.add(height+1);
            }
            if (node.right != null){
                q.add(node.right);
                hq.add(height+1);
            }
            if (height >= res.size()){
                res.add(new LinkedList<Integer>());
            }
            res.get(height).add(node.val);
        }
        return res;
    }

}
